package com.edushare.common.dao;

import java.util.HashMap;
import java.util.List;

import org.apache.cayenne.exp.Expression;
import org.apache.cayenne.query.SelectQuery;

import com.edushare.common.orm.ORMInterface;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Courses;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.EntityAttributes;
import com.edushare.common.vo.Universities;

public class EntityAttribSetDAO extends CayenneBasedDAO {

	public EntityAttribSetDAO(ORMInterface ormInterface) {
		super(ormInterface);
	}

	public void setUnivAttribute(Universities univ, String attribName,
			int attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_UNIV, univ
				.getUniversityId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_UNIV, univ
					.getUniversityId(), attribName);
		att.setAsInt(attribVal);
		getDataContext().commitChanges();
	}

	public void setDeptAttribute(Departments dept, String attribName,
			int attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_DEPT, dept
				.getDepartmentId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_DEPT, dept
					.getDepartmentId(), attribName);
		att.setAsInt(attribVal);
		getDataContext().commitChanges();
	}

	public void setCourseInstAttribute(CourseInstances cInst,
			String attribName, int attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_INST, cInst
				.getCourseInstanceId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_INST, cInst
					.getCourseInstanceId(), attribName);
		att.setAsInt(attribVal);
		getDataContext().commitChanges();
	}

	public void setCourseAttribute(Courses course, String attribName,
			int attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_COURSE,
				course.getCourseId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_COURSE, course
					.getCourseId(), attribName);
		att.setAsInt(attribVal);
		getDataContext().commitChanges();
	}

	public void setUnivAttribute(Universities univ, String attribName,
			String attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_UNIV, univ
				.getUniversityId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_UNIV, univ
					.getUniversityId(), attribName);
		att.setAsString(attribVal);
		getDataContext().commitChanges();
	}

	public void setDeptAttribute(Departments dept, String attribName,
			String attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_DEPT, dept
				.getDepartmentId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_DEPT, dept
					.getDepartmentId(), attribName);
		att.setAsString(attribVal);
		getDataContext().commitChanges();
	}

	public void setCourseInstAttribute(CourseInstances cInst,
			String attribName, String attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_INST, cInst
				.getCourseInstanceId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_INST, cInst
					.getCourseInstanceId(), attribName);
		att.setAsString(attribVal);
		getDataContext().commitChanges();
	}

	public void setCourseAttribute(Courses course, String attribName,
			String attribVal) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_COURSE,
				course.getCourseId(), attribName);
		if (att == null)
			att = createAttribute(EntityAttributes.OWNER_COURSE, course
					.getCourseId(), attribName);
		att.setAsString(attribVal);
		getDataContext().commitChanges();
	}

	public int getUnivAttributeInt(Universities univ, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_UNIV, univ
				.getUniversityId(), attribName);
		if (att == null)
			return 0;
		else
			return att.getAsInt();
	}

	public int getDeptAttributeInt(Departments dept, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_DEPT, dept
				.getDepartmentId(), attribName);
		if (att == null)
			return 0;
		else
			return att.getAsInt();
	}

	public int getCourseInstAttributeInt(CourseInstances cInst,
			String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_INST, cInst
				.getCourseInstanceId(), attribName);
		if (att == null)
			return 0;
		else
			return att.getAsInt();
	}

	public int getCourseAttributeInt(Courses course, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_COURSE,
				course.getCourseId(), attribName);
		if (att == null)
			return 0;
		else
			return att.getAsInt();
	}

	public String getUnivAttributeString(Universities univ, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_UNIV, univ
				.getUniversityId(), attribName);
		if (att == null)
			return "";
		else
			return att.getAsString();
	}

	public String getDeptAttributeString(Departments dept, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_DEPT, dept
				.getDepartmentId(), attribName);
		if (att == null)
			return "";
		else
			return att.getAsString();
	}

	public String getCourseInstAttributeString(CourseInstances cInst,
			String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_INST, cInst
				.getCourseInstanceId(), attribName);
		if (att == null)
			return "";
		else
			return att.getAsString();
	}

	public String getCourseAttributeString(Courses course, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_COURSE,
				course.getCourseId(), attribName);
		if (att == null)
			return "";
		else
			return att.getAsString();
	}

	public void removeUnivAttributeString(Universities univ, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_UNIV, univ
				.getUniversityId(), attribName);
		if (att != null)
			getDataContext().deleteObject(att);
		getDataContext().commitChanges();
	}

	public void removeDeptAttributeString(Departments dept, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_DEPT, dept
				.getDepartmentId(), attribName);
		if (att != null)
			getDataContext().deleteObject(att);
		getDataContext().commitChanges();
	}

	public void removeCourseInstAttributeString(CourseInstances cInst,
			String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_INST, cInst
				.getCourseInstanceId(), attribName);
		if (att != null)
			getDataContext().deleteObject(att);
		getDataContext().commitChanges();
	}

	public void removeCourseAttributeString(Courses cInst, String attribName) {
		EntityAttributes att = getAttribute(EntityAttributes.OWNER_COURSE,
				cInst.getCourseId(), attribName);
		if (att != null)
			getDataContext().deleteObject(att);
		getDataContext().commitChanges();
	}

	public EntityAttributes createAttribute(int ownerType, int ownerId,
			String attribName) {
		EntityAttributes att = new EntityAttributes();
		att.setAttribName(attribName);
		att.setAttribOwnerId(ownerId);
		att.setAttribOwnerType(ownerType);
		getDataContext().registerNewObject(att);
		return att;
	}

	public EntityAttributes getAttribute(int ownerType, int ownerId,
			String attribName) {
		List<EntityAttributes> att = getAttributes(ownerType, ownerId);
		for (EntityAttributes a : att) {
			if (a.getAttribName().equals(attribName)) {
				return a;
			}
		}
		return null;
	}

	@SuppressWarnings("unchecked")
	public List<EntityAttributes> getAttributes(int ownerType, int ownerId) {
		Expression exp = Expression.fromString("attribOwnerId=$attribOwnerId "
				+ "and attribOwnerType=$attribOwnerType");
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("attribOwnerId", ownerId);
		map.put("attribOwnerType", ownerType);
		SelectQuery query = new SelectQuery(EntityAttributes.class, exp
				.expWithParameters(map));
		return getDataContext().performQuery(query);
	}
}
